<template>
    <div class="app-container">
   <!--查询表单-->
<div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="24">
            <el-form-item label="分类名称">
              <el-input style="width: 100%" v-model="searchObj.name" placeholder="分类名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" icon="el-icon-search" size="mini"  @click="fetchPageList()">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>
        
            <!-- 工具条 -->
<div class="tools-div">
  <el-button type="success" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
</div>


<div class="app-container">
  <!-- 列表 -->
  <el-table
        v-loading="listLoading"
        :data="list"
        stripe
        border
        @selection-change="handleSelectionChange"
        style="width: 100%; margin-top: 10px"
      >
        <el-table-column type="selection" />

        <el-table-column label="序号" width="70" align="center">
          <template slot-scope="scope">
            {{ (page - 1) * limit + scope.$index + 1 }}
          </template>
        </el-table-column>

      <el-table-column prop="typeName" label="分类名称" />
      <el-table-column prop="description" label="描述"/>
      <el-table-column prop="createTime" label="创建时间" />
       <el-table-column prop="updateTime" label="修改时间" width="200"/>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)" title="修改"/>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)" title="删除"/>

        </template>
      </el-table-column>
    </el-table>
    </div>

<!-- 弹框 -->
    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%" >
      <el-form ref="dataForm" :model="sysCourseType" label-width="150px" size="small" style="padding-right: 40px;">
        <el-form-item label="分类名">
          <el-input v-model="sysCourseType.typeName"/>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="sysCourseType.description"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
      </span>
</el-dialog>


      <!-- 分页组件 -->
  <el-pagination
    :current-page="page"
    :total="total"
    :page-size="limit"
    style="padding: 30px 0; text-align: center;"
    layout="total, prev, pager, next, jumper"
    @current-change="fetchPageList"
  />


    </div>
</template>

<script>

// 导入 
import api from '@/api/sysCourseType/courseType.js'

export default {
   data(){
    return{
        list:[],
        page:1,
        limit:5,
        tota:0,
        searchObj:{},
        listLoading:true,
        dialogVisible:false,
        sysCourseType:{}
    }
   },
   created () {
    //this.listLoading=true;
     this.fetchPageList();
   },
   methods:{
    edit(id){
      //1.弹框
      this.dialogVisible=true;
      //2.赋值
      api.findCourseTypeById(id)
      .then(response=>{
        this.sysCourseType = response.data;
      })
    },
    saveOrUpdate(){
       if(this.sysCourseType.id!=null)
       {
         this.updateCourseType();
       }
       else
       {
        this.addCourseType();
       }
    },
    addCourseType(){
       api
       .addCourseType(this.sysCourseType)
       .then(response=>{
             this.$message({
                type: 'success',
                message: '添加成功!'
              });
           //关闭弹框
           this.dialogVisible=false;
           // 刷新列表
           this.fetchPageList();   

       })
    },
    updateCourseType(){
       api
       .updateCourseType(this.sysCourseType)
       .then(response=>{
             this.$message({
                type: 'success',
                message: '修改成功!'
              });
           //关闭弹框
           this.dialogVisible=false;
           // 刷新列表
           this.fetchPageList();   

       })
    },
    add(){
      this.dialogVisible=true;
      this.sysCourseType={};
    },
    removeDataById(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          api.removeCourseTypeById(id).then(response=>{
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.fetchPageList();
          })

        })
    },
    resetData(){
     //1.清空搜索条件
     this.searchObj={};
     //2.再次刷新列表
     this.fetchPageList();
    },
     fetchPageList(page=1){
        this.page=page;
        api.getCourseTypePageInfo(this.page,this.limit,this.searchObj).then(response=>{
            this.listLoading=false;
            // console.log(response);
            this.list = response.data.records;
            this.total = response.data.total;
        })
     }
   }
}
</script>

<style lang="less" scoped>

</style>